/**
 * @author lteng
 * @time 2022-06-17 20:36:43 
 */
 
package liteng.study.cn.stuscore.controller;

import java.io.IOException;
import java.net.URLDecoder;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import liteng.study.cn.stuscore.comm.Page;
import liteng.study.cn.stuscore.entity.EProject;
import liteng.study.cn.stuscore.service.EProjectService;
import liteng.study.cn.stuscore.service.impl.EProjectServiceImpl;

/**
 * 考试科目控制器
 */
@WebServlet("/project")
public class EProjectServlet extends HttpServlet {
	private EProjectService eProjectService = new EProjectServiceImpl();
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
	
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		request.setCharacterEncoding("UTF-8");
		System.out.println("project-get方法已启动");
		String action = request.getParameter("action");

		if (action.equals("info")) {
			descInfo(request, response);

		} else if (action.equals("page")) {
			pageInfo(request, response);

		} else if (action.equals("add")) {
			addInfo(request, response);

		} else if (action.equals("upd")) {
			updInfo(request, response);

		} else if (action.equals("del")) {
			delInfo(request, response);
		}
	}
	
	/**
	 * 
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	private void delInfo(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// TODO Auto-generated method stub
		int id = Integer.valueOf(request.getParameter("id"));

		EProject eProject = eProjectService.getOne(id);

		eProjectService.delete(eProject);

		response.sendRedirect("/stuscore/project?action=page&pageIndex=1&pageSize=10");

	}

	/**
	 * 
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void updInfo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String name = request.getParameter("name");
		String comm = request.getParameter("comm");
		int id = Integer.valueOf(request.getParameter("id"));

		EProject eProject = eProjectService.getOne(id);

		if (!name.equals(eProject.getName()) && isExist(name)) {
			request.setAttribute("error", "要修改的信息已存在！");

			request.getRequestDispatcher("project?action=info&id="+id).forward(request, response);
		} else {
			eProject.setName(name);
			eProject.setComm(comm);

			eProjectService.update(eProject);

			response.sendRedirect("/stuscore/project?action=page&pageIndex=1&pageSize=10");
		}
	}

	/**
	 * 
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void pageInfo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		int pageIndex = Integer.valueOf(request.getParameter("pageIndex"));
		int pageSize = Integer.valueOf(request.getParameter("pageSize"));
		String name = request.getParameter("name");
		if (name != null) {
			name = URLDecoder.decode(name, "utf-8");

		}
		Page page = null;
		if (name != null && !name.equals("")) {
			page = eProjectService.getPageEProjects(pageIndex, pageSize, name);
			
		} else {
			page = eProjectService.getPageEProjects(pageIndex, pageSize);


		}
		request.setAttribute("page", page);

		request.getRequestDispatcher("/pages/project/list.jsp").forward(request, response);

	}

	/**
	 * 修改年级信息
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void descInfo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub

		int id = Integer.valueOf(request.getParameter("id"));
		
		EProject eProject = eProjectService.getOne(id);
		request.setAttribute("project", eProject);
		request.getRequestDispatcher("/pages/project/upd.jsp").forward(request, response);

	}

	

	/**
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void addInfo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String name = request.getParameter("name");
		String comm = request.getParameter("comm");

		if (isExist(name)) {
			request.setAttribute("error", "要添加的信息已存在");
			request.getRequestDispatcher("/pages/project/add.jsp").forward(request, response);

		} else {
			EProject eProject = new EProject();
			eProject.setName(name);
			eProject.setComm(comm);

			eProjectService.add(eProject);

			response.sendRedirect("/stuscore/project?action=page&pageIndex=1&pageSize=10");
		}
	}

	/**
	 * 
	 * @param name
	 * @return
	 */
	private boolean isExist(String name) {
		// TODO Auto-generated method stub
		return eProjectService.isExistEProject(name);
	}
	
}
